{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tutorial 4: Generate Beaver Triples by Homomorphic Encryption\n",
    "In `Tutorial 0`, it demonstrated how to generate Beaver triples in the offline phase using a trusted third party. In this tutorial, we will illustrate how two parties can collaboratively generate Beaver triples and MSB Beaver triples using homomorphic encryption.\n",
    "First, import the following packages:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-21T11:21:41.207086Z",
     "start_time": "2024-03-21T11:21:39.075827Z"
    }
   },
   "outputs": [],
   "source": [
    "# import libraries\n",
    "from NssMPC.secure_model.mpc_party import SemiHonestCS\n",
    "from NssMPC.crypto.aux_parameter.beaver_triples import AssMulTriples\n",
    "from NssMPC.crypto.aux_parameter import BooleanTriples\n",
    "# from crypto.primitives.beaver.msb_triples import MSBTriples"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The SemiHonestCS is the computation parties, as explained in Tutorial 2. The BeaverTriples and MSBTriples are responsible for generating the corresponding Beaver triples and MSB triples, respectively. We will continue to use multiple threads to simulate the two parties."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-21T11:21:41.284250Z",
     "start_time": "2024-03-21T11:21:41.208120Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "TCPServer waiting for connection ......\n",
      "TCPServer waiting for connection ......\n",
      "successfully connect to server 127.0.0.1:8000\n",
      "TCPServer successfully connected by :('127.0.0.1', 9100)\n",
      "successfully connect to server 127.0.0.1:9000\n",
      "TCPServer successfully connected by :('127.0.0.1', 8200)\n"
     ]
    }
   ],
   "source": [
    "import threading\n",
    "\n",
    "# set Server\n",
    "server = SemiHonestCS(type='server')\n",
    "\n",
    "server.set_multiplication_provider()\n",
    "server.set_comparison_provider()\n",
    "server.set_nonlinear_operation_provider()\n",
    "\n",
    "def set_server():\n",
    "    # CS connect\n",
    "    server.online()\n",
    "\n",
    "# set Client\n",
    "client = SemiHonestCS(type='client')\n",
    "\n",
    "client.set_multiplication_provider()\n",
    "client.set_comparison_provider()\n",
    "client.set_nonlinear_operation_provider()\n",
    "\n",
    "def set_client():\n",
    "    # CS connect\n",
    "    client.online()\n",
    "\n",
    "\n",
    "server_thread = threading.Thread(target=set_server)\n",
    "client_thread = threading.Thread(target=set_client)\n",
    "\n",
    "server_thread.start()\n",
    "client_thread.start()\n",
    "client_thread.join()\n",
    "server_thread.join()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Generate the beaver triples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-21T11:21:41.422323Z",
     "start_time": "2024-03-21T11:21:41.285295Z"
    }
   },
   "outputs": [],
   "source": [
    "import threading\n",
    "from NssMPC.crypto.aux_parameter.beaver_triples import AssMulTriples\n",
    "def server_gen_beaver():\n",
    "    AssMulTriples.gen_and_save(10, 2,type_of_generation = 'HE',party = server)  # num_of_triples, num_of_party, protocol, party\n",
    "\n",
    "\n",
    "def client_gen_beaver():\n",
    "    AssMulTriples.gen_and_save(10, 2,type_of_generation = 'HE',party = client)  # num_of_triples, num_of_party, protocol, party\n",
    "\n",
    "\n",
    "server_thread = threading.Thread(target=server_gen_beaver)\n",
    "client_thread = threading.Thread(target=client_gen_beaver)\n",
    "\n",
    "server_thread.start()\n",
    "client_thread.start()\n",
    "client_thread.join()\n",
    "server_thread.join()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Generate the MSB beaver triples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-21T11:21:41.437628Z",
     "start_time": "2024-03-21T11:21:41.423357Z"
    }
   },
   "outputs": [],
   "source": [
    "def server_gen_msb_beaver():\n",
    "    BooleanTriples.gen_and_save(10, 2, type_of_generation = 'HE',party = server)\n",
    "\n",
    "\n",
    "def client_gen_msb_beaver():\n",
    "    BooleanTriples.gen_and_save(10, 2, type_of_generation = 'HE',party = client)\n",
    "\n",
    "\n",
    "server_thread = threading.Thread(target=server_gen_msb_beaver)\n",
    "client_thread = threading.Thread(target=client_gen_msb_beaver)\n",
    "\n",
    "server_thread.start()\n",
    "client_thread.start()\n",
    "client_thread.join()\n",
    "server_thread.join()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
